package com.group2.view;
import static model.netBase.IP;
import javax.swing.JPanel;
import javax.swing.JCheckBox;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Vector;

import model.*;
public class BankAccountFrame extends JPanel {
	private JTable Billtable;
	private JCheckBox IncomeCheck;
	private JCheckBox ExpendCheck;
	private JCheckBox ThisMonthCheck;
	private static ArrayList<JournalAccount>list=null;
	private static Account MyAccount=null;
	private static String studentId=null;
	/**
	 * Create the panel.
	 */
	public BankAccountFrame(String id,Account mine) throws IOException, ClassNotFoundException {
		studentId=id;
		MyAccount=mine;
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "\u7B5B\u9009\u6761\u4EF6", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		
		JScrollPane scrollPane = new JScrollPane();
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(33)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 305, GroupLayout.PREFERRED_SIZE)
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 303, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(34, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(panel, GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 135, GroupLayout.PREFERRED_SIZE)
					.addGap(22))
		);
		
		Billtable = new JTable();
		Billtable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"消费项", "\u7C7B\u578B", "金额", "\u4EA4\u6613\u65F6\u95F4"
			}
		));
		scrollPane.setViewportView(Billtable);
		
		IncomeCheck = new JCheckBox("收入");

		ExpendCheck = new JCheckBox("支出");

		ThisMonthCheck = new JCheckBox("本月");
		
		JButton btnNewButton = new JButton("查询");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					billsearchActionPerformed(e);
				} catch (IOException ex) {
					throw new RuntimeException(ex);
				} catch (ClassNotFoundException ex) {
					throw new RuntimeException(ex);
				}
			}
		});
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(IncomeCheck)
					.addGap(18)
					.addComponent(ExpendCheck)
					.addGap(18)
					.addComponent(ThisMonthCheck)
					.addGap(18)
					.addComponent(btnNewButton)
					.addContainerGap(14, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(IncomeCheck)
						.addComponent(ExpendCheck)
						.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
							.addComponent(ThisMonthCheck)
							.addComponent(btnNewButton)))
					.addContainerGap(92, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		setLayout(groupLayout);
		fillbilltable(MyAccount.Accountlist);
	}
	/**
	 * 按条件拉取流水信息
	 */
	public void refresh() throws IOException, ClassNotFoundException {
		String In= String.valueOf(IncomeCheck.isSelected());
		String Out= String.valueOf(ExpendCheck.isSelected());
		String ThisMonth= String.valueOf(ThisMonthCheck.isSelected());
		System.out.println("开始传输");
		//拉取选课队列
		Socket socket = new Socket(IP(), 2000);//此处主机地址请自行修改
		//设置超时时间
		socket.setSoTimeout(3000);
		//构建IO
		OutputStream os = socket.getOutputStream();
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
		bw.write("62" + '\n'+studentId+'\n'+In+'\n'+Out+'\n'+ThisMonth+'\n');
		bw.flush();
		//从服务端输入到本地
		InputStream is = socket.getInputStream();
		ObjectInputStream in = new ObjectInputStream(is);
		Object obj = in.readObject();
		System.out.println("传输结束");
		list= (ArrayList<JournalAccount>) obj;
		//输出输入流关闭
		is.close();
		os.close();
		socket.close();
		fillbilltable(list);
	}
	/**
	 * 初始化表格数据
	 */
	private void fillbilltable(ArrayList<JournalAccount>course) {
		DefaultTableModel dtm=(DefaultTableModel) Billtable.getModel();
		dtm.setRowCount(0);//设为0行
		//连接数据库
		try {
			int size = course.size();
			int index = 0;
			while (size > 0) {
				Vector v = new Vector();
				v.add(course.get(index).getItem()) ;
				if(course.get(index).getType().equals("true"))v.add("收入");
				else v.add("支出");
				v.add(course.get(index).getAmount());v.add(course.get(index).getAccountTime());
				dtm.addRow(v);
				size--;
				index++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 账单查询事件处理
	 *
	 */
	private void billsearchActionPerformed(ActionEvent e) throws IOException, ClassNotFoundException {
		Boolean ifIncome= IncomeCheck.isSelected();//是否为收入
		Boolean ifExpend= ExpendCheck.isSelected();//是否为输出
		Boolean ifThisMonth= ThisMonthCheck.isSelected();//是否为本月
		
		//连接数据库
		refresh();
	}

}
